<?php
/**
* 门店信息插件
* @category apibusiness
* @package apibusiness/response/plugin/order
* @author wangjianjun<wangjianjun@shopex.cn>
* @version 0.1: tbstore.php 2016-9-21
*/
class apibusiness_response_order_plugin_tbstore extends apibusiness_response_order_plugin_abstract
{
    /**
     * 订单更新后操作
     * @return void
     * @author 
     **/
    public function postUpdate(){
        
        //阿里全渠道 门店字段 omnichannel_param 
        if($this->_platform->_ordersdf["omnichannel_param"]){
            //根据订单返回信息识别，当前订单门店处理情况 不管自提还是配送 拒单还是已发货 都只拿第一组数据 目前不考虑部分配送或者自提的情况出现 
            $result_arr = $this->getOmnichannelParamArr($this->_platform->_ordersdf["omnichannel_param"]);
            if($result_arr[0]){
                $this->deliveryOmnichannel($result_arr[0],$this->_platform->_ordersdf["order_bn"],$this->_platform->_ordersdf["shop_id"]);
            }
        }
        
    }
    
    //门店自提 门店配送 拒单或者已发货的处理
    private function deliveryOmnichannel($omnichannel_param,$order_bn,$shop_id){
        if ($omnichannel_param["orderType"] == "STORE_DELIVER"){
            //门店配送
            switch($omnichannel_param["orderState"]){
                case "X_SHOP_DENY":
                    $this->deliveryOmnichannelCancel($order_bn,$shop_id);
                    break;
                case "X_SHOP_HANDLED":
                    $this->deliveryOmnichannelDid($order_bn,$shop_id);
                    break;
            }
        }
        if($omnichannel_param["orderType"] == "STORE_COLLECT"){
            //门店自提
            switch($omnichannel_param["orderState"]){
                case "X_SHOP_DENY":
                    $this->deliveryOmnichannelCancel($order_bn,$shop_id);
                    break;
                case "X_STORE_COLLECT_CONSUMED":
                    $this->deliveryOmnichannelDid($order_bn,$shop_id);
                    break;
            }
        }
    }
    
    //取消发货单
    private function deliveryOmnichannelCancel($order_bn,$shop_id){
        //获取订单id
        $mdlOmeOrders = app::get('ome')->model('orders');
        $rs_order = $mdlOmeOrders->dump(array("order_bn"=>$order_bn,"shop_id"=>$shop_id),"order_id");
        //获取发货单
        $deliveryObj = app::get('ome')->model('delivery');
        $deliveryIds = $deliveryObj->getDeliverIdByOrderId($rs_order["order_id"]);
        $delivery_list = $deliveryObj->getList('delivery_bn',array('delivery_id|in'=>$deliveryIds,'process'=>'false'));
        foreach ($delivery_list as $var_d){
            $data = array(
                    'status'=>'cancel',
                    'memo'=>'阿里全渠道门店拒绝接单，取消发货单！',
                    'delivery_bn'=>$var_d['delivery_bn'],
            );
            kernel::single('ome_event_receive_delivery')->update($data);
        }
    }
    
    //发货
    private function deliveryOmnichannelDid($order_bn,$shop_id){
        //获取订单id
        $mdlOmeOrders = app::get('ome')->model('orders');
        $rs_order = $mdlOmeOrders->dump(array("order_bn"=>$order_bn,"shop_id"=>$shop_id),"order_id");
        //获取发货单
        $deliveryObj = app::get('ome')->model('delivery');
        $deliveryIds = $deliveryObj->getDeliverIdByOrderId($rs_order["order_id"]);
        $delivery_list = $deliveryObj->getList('delivery_bn,logi_id,weight,delivery_cost_actual',array('delivery_id|in'=>$deliveryIds,'process'=>'false'));
        foreach ($delivery_list as $var_d){
            $data = array(
                    'status'=>'delivery',
                    'delivery_bn'=>$var_d['delivery_bn'],
                    'delivery_time'=>time(),
                    'weight'=>$var_d['weight'],
                    'delivery_cost_actual'=>$var_d['delivery_cost_actual'],
                    'logi_id'=>$var_d['logi_id'],
                    'logi_no'=>$var_d['delivery_bn'],
            );
            kernel::single('ome_event_receive_delivery')->update($data);
        }
    }
    
    //根据返回的字符串 格式化 返回数组
    private function getOmnichannelParamArr($omnichannel_param){
        $tm_omnichannel = explode(";",$omnichannel_param);
        $result_arr = array();
        foreach($tm_omnichannel as $t_o){
            if(!$t_o){
                continue;
            }
            $tm_omnichannel_d = explode(",",$t_o);
            $t_result_arr = array();
            foreach ($tm_omnichannel_d as $t_o_d){
                if(!$t_o_d){
                    continue;
                }
                $tm_omnichannel_m = explode(":",$t_o_d);
                $count_o_m = count($tm_omnichannel_m);
                if($count_o_m > 2){
                    //acceptExpirationTime这种 value中有冒号 拼接value
                    $t_value = "";
                    $t_c_key = $count_o_m - 1;
                    for ($f_i = 1;$f_i<=$t_c_key;$f_i++){
                        if($f_i == 1){
                            $t_value = $tm_omnichannel_m[$f_i];
                        }else{
                            $t_value .= ":".$tm_omnichannel_m[$f_i];
                        }
                    }
                    $t_result_arr[$tm_omnichannel_m[0]] = $t_value;
                }else{
                    //正常的key value用冒号分割
                    $t_result_arr[$tm_omnichannel_m[0]] = $tm_omnichannel_m[1];
                }
            }
            $result_arr[] = $t_result_arr;
        }
        return $result_arr;
    }
    
}